package com.wendyxie.sproutup.view

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.marginEnd
import androidx.core.view.setMargins
import com.wendyxie.sproutup.databinding.ActivityParentPwdBinding
import com.wendyxie.sproutup.db.SQLInitHelper
import com.wendyxie.sproutup.db.SQLUserHelper
import com.wendyxie.sproutup.utils.DimensionUtils
import com.wendyxie.sproutup.utils.SPUtil
import com.wendyxie.sproutup.view.widget.PwdEditLinearLayout


class ParentPwdActivity : AppCompatActivity() {
    private lateinit var binding: ActivityParentPwdBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityParentPwdBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.llPwd.onTextCompletedListener = object : PwdEditLinearLayout.OnTextCompletedListener() {
            override fun onTextCompleted(text: String) {
                // 获取用户配置的密码
                val userEntity = SQLUserHelper.queryBabyById(SPUtil.getBabyId(binding.root.context), SQLInitHelper(binding.root.context).openDatabase())
                if (text == userEntity.babyPwd) {
                    Toast.makeText(this@ParentPwdActivity, "密码正确", Toast.LENGTH_SHORT).show()
                    startActivity(Intent(this@ParentPwdActivity, ParentSettingActivity::class.java))
                    finish()
                } else {
                    Toast.makeText(this@ParentPwdActivity, "密码错误", Toast.LENGTH_LONG).show()
                }
            }
        }

        // 修改密码
        binding.tvChangePwd.paint.isUnderlineText = true
        binding.tvChangePwd.setOnClickListener {
            startActivity(Intent(this, ParentPwdChangeActivity::class.java))
        }
    }
}
